use serde::{Serialize, Deserialize};
use super::app_files::AppFiles;
use super::version_info::VersionInfo;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all="camelCase")]
pub struct LogItem {
  pub log_date: String,
  pub version_info: Option<VersionInfo>,
  pub app_files: AppFiles,
  pub remark_content: Option<String>,
}

impl LogItem {
  pub fn new(log_date: String, version_info: Option<VersionInfo>, app_files: AppFiles, remark_content: Option<String>) -> Self {
    return LogItem { 
      log_date, 
      version_info, 
      app_files, 
      remark_content
    };
  }

  pub fn simple_new(version_info: Option<VersionInfo>, app_files: AppFiles) -> Self {
    let log_date = chrono::Local::now().format("%Y-%m-%d %H:%M:%S").to_string();
    let remark_content: Option<String> = None;
    return Self::new(log_date, version_info, app_files, remark_content)
  }

  pub fn update_remark_content(&mut self, remark_content: String) {
    self.remark_content = Some(remark_content);
  }
  pub fn eq(&self, other: &Self) -> bool {
    if self.log_date == other.log_date {
      return true;
    }
    if self.app_files.get_is_same(&other.app_files) {
      return self.is_eq_version(other.version_info.clone());
    }
    return false;  
  }

  pub fn is_eq_version(&self, other_version: Option<VersionInfo>) -> bool {
    if self.version_info.is_none() && other_version.is_none() {
      return true;
    }
    if other_version.is_none() || self.version_info.is_none() {
      return false;
    }
    return self.version_info.clone().unwrap().is_eq(&other_version.unwrap());
  }
}
